package com.example.pt.controller;

import com.example.pt.dto.ApiResponse;
import com.example.pt.entity.Torrent;
import com.example.pt.entity.User;
import com.example.pt.repository.PeerRepository;
import com.example.pt.repository.TorrentRepository;
import com.example.pt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TorrentRepository torrentRepository;

    @Autowired
    private PeerRepository peerRepository;

    @PostMapping("/register")
    public ApiResponse<String> register(@RequestBody User user) {
        //ApiResponse<String> result = userService.register(user);
        return userService.register(user);
    }

    @PostMapping("/login")
    public ApiResponse<User> login(@RequestBody User user) {
        User loginUser = userService.login(user.getUsername(), user.getPassword());
        if (loginUser == null) {
            return ApiResponse.fail("用户名或密码错误");
        }
        //更新用户等级
        userService.checkAndUpgradeUserLevel(loginUser);

        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 判断是否已经加过分
        if (loginUser.getLast_login() == null || !loginUser.getLast_login().isEqual(today)) {
            Float currentScore = loginUser.getScore() != null ? loginUser.getScore() : 0f;
            loginUser.setScore(currentScore + 5f);
            loginUser.setLast_login(today);
            userService.updateUser(loginUser); // 持久化保存
        }
        return ApiResponse.success(loginUser);
    }

    @GetMapping("/profile/{user_id}")
    public ApiResponse<User> getProfile(@PathVariable("user_id") Long userId) {
        User userP = userService.getUserProfile(userId);
        return ApiResponse.success(userP);
    }

    /**
     * 获取某个用户上传的种子列表
     */
    @GetMapping("/torrents/{userId}")
    public ResponseEntity<List<Torrent>> getTorrentsByUserId(@PathVariable Long userId) {
        List<Torrent> torrents = torrentRepository.findByUploaderId(userId);
        return ResponseEntity.ok(torrents);
    }

    @PostMapping("/torrents/delete")
    public ResponseEntity<?> deleteTorrentById(@RequestParam Long id) {
        int deletedCount = torrentRepository.deleteByIdValue(id);
        if (deletedCount == 0) {
            return ResponseEntity.status(404).body("种子不存在");
        }
        return ResponseEntity.ok("删除成功");
    }

    /**
     * 获取某个用户下载的种子列表
     */
    @GetMapping("/downloads/{userId}")
    public ResponseEntity<List<Torrent>> getDownloadsByUserId(@PathVariable Long userId) {

        List<Torrent> torrents = peerRepository.findDownloadsByUserId(userId);
        return ResponseEntity.ok(torrents);
    }

    @DeleteMapping("/downloads/delete")
    public ResponseEntity<?> deleteDownloadById(@RequestParam Long id) {
        peerRepository.deleteById(id);

        return ResponseEntity.ok("删除成功");
    }



}